package com.example.config.Utility;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.SocketException;
import java.util.ArrayList;

@Slf4j
public class FtpUtil {
    private static final ArrayList arrayList = new ArrayList();

    /**
     * @param ftpIp       FTP IP
     * @param ftpUserName FTP 用户名
     * @param ftpPassword FTP密码
     * @param ftpPort     FTP 端口号
     * @param localPath   本地路径
     */
    public void downloadFtpFile(String ftpIp, String ftpUserName, String ftpPassword, int ftpPort, String localPath) {
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient(ftpIp, ftpUserName, ftpPassword, ftpPort);
            ftpClient.setControlEncoding("UTP-8");
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //判断本地路径是否存在，不存在进行创建
            File file1 = new File(localPath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
//            获取所有文件列表
            for (Object arrayList1 : arrayList) {
                File localFile = new File(localPath + arrayList1);
                OutputStream os = new FileOutputStream(localFile);
                ftpClient.enterLocalPassiveMode();
                ftpClient.retrieveFile(String.valueOf(arrayList1), os);
                os.close();
                ftpClient.hashCode();
                PrintUtil pul = new PrintUtil();
                pul.printImage(localPath + arrayList1);
                ftpClient.dele(String.valueOf(arrayList1));
            }

        } catch (FileNotFoundException e) {
            System.out.println("没有找到文件");
            log.error("没有找到要下载的文件");
        } catch (SocketException e) {
            System.out.println("连接失败");
            log.error("连接下载文件的FTP失败");
        } catch (IOException e) {
            System.out.println("读写异常");
            log.error("读写文件异常");
        }
    }


    /**
     * @param ftpIp       FTP的IP
     * @param ftpUserName ftp用户名
     * @param ftpPassword ftp密码
     * @param ftpPort     ftp端口号
     * @return
     */
    public FTPClient getFTPClient(String ftpIp, String ftpUserName, String ftpPassword, int ftpPort) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(ftpIp, ftpPort);
            ftpClient.login(ftpUserName, ftpPassword);
            ftpClient.setDefaultTimeout(3000);//设置默认超时时间
            ftpClient.setConnectTimeout(3000);//设置连接超时时间
            ftpClient.setDataTimeout(3000);//设置读取数据超时时间（每次socket传输，不是指整个下载）
            ftpClient.setControlKeepAliveTimeout(60); // 设置ftp在上传或下载大文件时，保持ftp的control的socket连接keepalive
            ftpClient.enterLocalPassiveMode();//设置被动模式，文件传输端口设置
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP");
            } else {
                System.out.println("连接成功");
                FTPFile[] fle = ftpClient.listFiles();
                for (FTPFile fus : fle) {
                    if (fus.getName().contains("jpg")) {
                        arrayList.add(fus.getName());
                    } else if (fus.getName().contains("png")) {
                        arrayList.add(fus.getName());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
            log.error("连接下载文件的FTP失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftpClient;
    }

}